/**
 * 
 */
package com.qiyou.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.qiyou.dao.IBlogDao;
import com.qiyou.entity.Blog;
import com.qiyou.util.DBUtil;
import com.qiyou.util.TransformUtil;

/**
 * 博客数据操作实现
 * 
 * @author 赛艳志
 * 
 */
public class BlogDaoImpl implements IBlogDao {
    private Connection conn;

    public BlogDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(Blog blog) throws SQLException {
        String sql = "insert into tb_blog(userID,blogTitle,blogContent,blogDate,blogState,blogNums) values(?,?,?,?,?,?)";
        Object[] objects = TransformUtil.beanTOArray(blog, true);
        return DBUtil.execute(conn, sql, objects);
    }

    @Override
    public boolean doUpdate(Blog blog) throws SQLException {
        String sql = "update tb_blog set blogID=?,userID=?,blogTitle=?,blogContent=?,blogDate=?,blogState=?,blogNums=? where blogID=?";
        Object[] objects = TransformUtil.beanTOArray(blog, false);
        return DBUtil.execute(conn, sql, objects,objects[0]);
    }

    @Override
    public boolean doDelete(long blogID) throws SQLException {
        String sql = "delete from tb_blog where blogID=?";
        return DBUtil.execute(conn, sql, blogID);
    }

    @Override
    public Blog getBlog(long blogID) throws SQLException {
        Blog blog = null;
        String sql = "select * from tb_blog where blogID=?";
        List<Object[]> all = DBUtil.query(conn, sql, blogID);
        if (null!=all&&all.size()>0) {
            blog = (Blog) TransformUtil.arrayToBean(all.get(0), Blog.class);
        }
        return blog;
    }

    @Override
    public List<Blog> getBlogs(String userID) throws SQLException {
        List<Blog> blist = null;
        String sql = "select * from tb_blog where userID=?";
        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (null!=all&&all.size()>0) {
            blist = new ArrayList<Blog>(all.size());
            Blog blog = null;
            for (Object[] objects : all) {
                blog = (Blog) TransformUtil.arrayToBean(objects, Blog.class);
                blist.add(blog);
            }
        }
        return blist;
    }

    @Override
    public List<Blog> getBlogs(Object... params) throws SQLException {
        List<Blog> blist = null;
        StringBuffer sql = new StringBuffer("select * from tb_blog ");
        if (null != params && params.length > 0) {
            for (Object param : params) {
                sql.append(param);
            }
        }
        List<Object[]> all = DBUtil.query(conn, sql.toString());
        if (null!=all&&all.size()>0) {
            blist = new ArrayList<Blog>(all.size());
            Blog blog = null;
            for (Object[] objects : all) {
                blog = (Blog) TransformUtil.arrayToBean(objects, Blog.class);
                blist.add(blog);
            }
        }
        return blist;
    }

}
